﻿<?php

     //-- 使用SSL連接，請確認php.ini中extension=php_openssl.dll有開啟
     //-- 本範例測試環境為PHP5217，確認php.ini中register_long_arrays=Off

     function getParameter($pname){
          return isset($_POST[$pname])?$_POST[$pname]:"";
     }

     echo "<pre>";
     $transurl = "https://maple2.neweb.com.tw:443/CashSystemFrontEnd/Payment";
     $code = "abcd1234";
     $merchantnumber = getParameter('merchantnumber');
     $ordernumber = getParameter('ordernumber');
     $amount = getParameter('amount');
     $paymenttype = getParameter('paymenttype');
     $paytitle = getParameter('paytitle');
     $paymemo = getParameter('paymemo');
     $bankid = getParameter('bankid');

	$duedate = getParameter('duedate');
	$payname = getParameter('payname');
	$payphone = getParameter('payphone');
     $returnvalue = (getParameter('returnvalue')=="1");
     $reprint = (getParameter('reprint')=="1");
     $plaintext = $merchantnumber.$code.$amount.$ordernumber;
     $hash = md5($plaintext);
     $nexturl = getParameter('nexturl');
     //echo "plaintext=[".$plaintext."]";

     $postdata = "merchantnumber=".$merchantnumber.
                 "&ordernumber=".$ordernumber.
                 "&amount=".$amount.
                 "&paymenttype=".$paymenttype.
                 "&bankid=".$bankid.
                 "&paytitle=".urlencode($paytitle).
                 "&paymemo=".urlencode($paymemo).
                 "&payname=".urlencode($payname).
                 "&payphone=".$payphone.
                 "&duedate=".$duedate.
                 "&returnvalue=".$returnvalue.
                 "&hash=".$hash.
                 "&nexturl=".urlencode($nexturl);

     $url = parse_url($transurl);
     
     $postdata = "POST ".$url['path']." HTTP/1.0\r\n".
               "Content-Type: application/x-www-form-urlencoded\r\n".
               "Host: ".$url['host']."\r\n".
               "Content-Length: ".strlen($postdata)."\r\n".
               "\r\n".
               $postdata;

     //echo "<br>postdata=".$postdata."\n";

     //echo "<br>host=".$url['host'].",port=".$url['port'];
     $receivedata = "";

     //-- 若不用SSL(https)連接，則改為 $fp = fsockopen ($url['host'], $url['port'], $errno, $errstr, 90);
     $fp = fsockopen ("sslv3://".$url['host'], $url['port'], $errno, $errstr, 90);
     if(!$fp) { 
          echo "$errstr ($errno)<br>\n";
     }else{ 
          fputs ($fp, $postdata);

          do{ 
               if(feof($fp)) break;
               $tmpstr = fgets($fp,128);
               $receivedata = $receivedata.$tmpstr;
          }while(true);
          fclose ($fp);
     }



     $receivedata = str_replace("\r","",trim($receivedata));
     
     $isbody = false;
     $httpcode = null;
     $httpmessage = null;
     $result = "";
     $array1 = explode("\n",$receivedata);
     for($i=0;$i<count($array1);$i++){
          if($i==0){
               $array2 = explode(" ",$array1[$i]);
               $httpcode = $array2[1];
               $httpmessage = $array2[2];
          }else if(!$isbody){
               if(strlen($array1[$i])==0) $isbody = true;
          }else{
               $result = $result.$array1[$i];
          }
     }

     if($httpcode!="200"){
          if($httpcode=="404") echo "網址錯誤，無法找到網頁!";
          else if($httpcode=="500") echo "伺服器錯誤!";
          else echo $httpmessage;
          return;
     }

     //echo "receivedata==>".$receivedata."\n";
     //echo "result==>".$result."\n";
     
     //-- 分割資料取參數
     $ary1 = explode("&",$result);
     for($i=0;$i<count($ary1);$i++){
          $ary2 = explode("=",$ary1[$i]);
          echo $ary2[0]."=".$ary2[1]."\n";
     }
     
     echo "</pre>";
?>